package com.ddyx.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.common.util.ResponseJson;
import com.ddyx.entity.Device;
import com.ddyx.entity.IpcCode;
import com.ddyx.entity.Store;
import com.ddyx.mapper.DeviceMapper;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.service.IDeviceService;

/**
 * @ClassName: DeviceServiceImpl.java
 * @Module: 设备管理逻辑实现模块
 * @Description: 
 * 
 * @author: QingGuan.Kong
 * @date: 2017年9月12日 下午13:53:10
 * 
 */
@Service("deviceService")
public class DeviceServiceImpl implements IDeviceService{

	@Resource
	DeviceMapper deviceMapper;
	@Resource
	StoreMapper storeMapper;
	
	@Override
	public Map<String, Object> addDevice(int storeId, String deviceName, String ipcId, String ip, int port) {
		Map<String, Object> map = new HashMap<>();
		Device device = new Device();
		device.setDeviceName(deviceName);
		device.setIp(ip);
		device.setIpcId(ipcId);
		device.setPort(port);
		device.setStoreId(storeId);
		if(deviceMapper.addDevice(device)) {
			map.put("status", true);
			map.put("message", "添加成功");
		}else {
			map.put("status", false);
			map.put("message", "添加失败，请稍后重试");
		}
		return map;
	}

	@Override
	public Map<String, Object> updateDevice(int deviceId, int storeId, String deviceName, String ipcId, String ip, int port) {
		Map<String, Object> map = new HashMap<>();
		Device device = new Device();
		device.setDeviceId(deviceId);
		device.setDeviceName(deviceName);
		device.setIp(ip);
		device.setIpcId(ipcId);
		device.setPort(port);
		device.setStoreId(storeId);
		if(deviceMapper.updateDevice(device)) {
			map.put("status", true);
			map.put("message", "修改成功");
		}else {
			map.put("status", false);
			map.put("message", "修改失败，请稍后重试");
		}
		return map;
	}

	@Override
	public String selectIpcId(int page, int size, String ipcId) {
		int start = 0;
		if(size<1)
			size=20;
		if(page<1)
			page = 1;
		start = (page-1)*size;
		int total = deviceMapper.ipcIdCount(start, size, ipcId);
		if(total<1)
			return ResponseJson.getInstance().toString();
		List<Map<String,Object>> list = deviceMapper.selectipcIdAll(start, size, ipcId);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();
	}

	@Override
	public String selectDevice(int page, int size, String deviceName, String ipcId, String storeName) {
		int start = 0;
		if(size<1)
			size=20;
		if(page<1)
			page = 1;
		start = (page-1)*size;
		int total = deviceMapper.deviceCount(start, size, deviceName, ipcId, storeName);
		if(total<1)
			return ResponseJson.getInstance().toString();
		List<Map<String,Object>> list = deviceMapper.selectDeviceAll(start, size, deviceName, ipcId, storeName);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();
	}

	@Override
	public Map<String, Object> select(Map<String, Object> param) {
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> ipCodeList = deviceMapper.selectipcIdAll(0, 10, "");
		List<Store> storeList = storeMapper.selectByStoreName(param);
		map.put("ipCodeList", ipCodeList);
		map.put("storeList", storeList);
		return map;
	}
}
